package com.hightemperator.service;

import com.hightemperator.dao.*;
import com.hightemperator.po.*;
import com.hightemperator.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class ReleaseService {

    @Autowired
    public ActivityLableMapper activityLableMapper;
    @Autowired
    public ActivityMapper activityMapper;
    @Autowired
    public CircleMapper circleMapper;
    @Autowired
    public JoinActivityMapper joinActivityMapper;
    @Autowired
    public MessageMapper messageMapper;

    /**
     * 获取活动标签
     *
     * @return
     */
    public List<ActivityLable> getLableList() {
        ActivityLableExample example = new ActivityLableExample();
        return activityLableMapper.selectByExample(example);
    }

    public int getCanRelease(Integer id){
        Activity preActivity = activityMapper.getPreActivity(id);
        Date date = new Date();
        DateUtils dateUtils = new DateUtils();
        if (preActivity != null) {
            long minute = dateUtils.getHoursBetweenDays(preActivity.getCreatetime(), date);
            int hour = date.getHours();
            if (Math.abs(minute) > 120 && (hour <= 20 && hour >= 8)) {
                return 1;
            }
        }
        return 0;
    }

    /**
     * 添加活动
     *
     * @param activity
     * @return
     */
    public int addActivity(Activity activity) {
        int successCode = activityMapper.insertSelective(activity);
        return successCode;
    }

    /**
     * 修改活动
     * 如果修改活动操作成功那么通知所有已经参加活动的人活动变更的消息
     *
     * @param activity
     * @return
     */
    public int updateActivity(Activity activity) {

        int successCode = activityMapper.updateByPrimaryKeySelective(activity);
        if (successCode != 0) {
            JoinActivityExample example = new JoinActivityExample();
            JoinActivityExample.Criteria criteria = example.createCriteria();
            if (activity.getId() == null) {
                return 0;
            }
            criteria.andActivityidEqualTo(activity.getId());
            List<JoinActivity> joinActivities = joinActivityMapper.selectByExample(example);
            List<Message> messages = new ArrayList<>();
            for (JoinActivity joinActivity : joinActivities) {
                Message message = new Message();
                message.setReciveid(joinActivity.getUserid());
                message.setCreatorid(joinActivity.getCreateuserid());
                message.setType("4");
                message.setContent("aaaaa");
                message.setCircleid(activity.getId());
                messages.add(message);
            }
            successCode = messageMapper.insertBatch(messages);
        }
        return successCode;
    }

    /**
     * 添加动态
     *
     * @param circle
     * @return
     */
    public int addCircle(Circle circle) {
        return circleMapper.insertSelective(circle);
    }

}